<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>基于法线微分的分割 | 荒岛</title><meta name="author" content="Fi9zero"><meta name="copyright" content="Fi9zero"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="基于法线微分的分割🔹 1. DoN (Difference of Normals) 算法原理 核心思想：通过计算不同尺度下的法线差异来提取几何特征。 步骤： 在小尺度下计算法线：反映局部精细细节（如边缘、小物体）。 在大尺度下计算法线：反映宏观表面趋势（忽略小细节）。 计算两者之差（DoN）：DoN &#x3D; Normal_large - Normal_small。 DoN 的模长（或曲率）可以突出尺">
<meta property="og:type" content="article">
<meta property="og:title" content="基于法线微分的分割">
<meta property="og:url" content="https://ckyfi9zero.github.io/2025/08/05/2025-08-05-%E5%9F%BA%E4%BA%8E%E6%B3%95%E7%BA%BF%E5%BE%AE%E5%88%86%E7%9A%84%E5%88%86%E5%89%B2/index.html">
<meta property="og:site_name" content="荒岛">
<meta property="og:description" content="基于法线微分的分割🔹 1. DoN (Difference of Normals) 算法原理 核心思想：通过计算不同尺度下的法线差异来提取几何特征。 步骤： 在小尺度下计算法线：反映局部精细细节（如边缘、小物体）。 在大尺度下计算法线：反映宏观表面趋势（忽略小细节）。 计算两者之差（DoN）：DoN &#x3D; Normal_large - Normal_small。 DoN 的模长（或曲率）可以突出尺">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://ckyfi9zero.github.io/image/avatar.jpg">
<meta property="article:published_time" content="2025-08-05T06:29:23.000Z">
<meta property="article:modified_time" content="2025-08-05T07:23:23.970Z">
<meta property="article:author" content="Fi9zero">
<meta property="article:tag" content="c++">
<meta property="article:tag" content="PCL">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://ckyfi9zero.github.io/image/avatar.jpg"><script type="application/ld+json">{
  "@context": "https://schema.org",
  "@type": "BlogPosting",
  "headline": "基于法线微分的分割",
  "url": "https://ckyfi9zero.github.io/2025/08/05/2025-08-05-%E5%9F%BA%E4%BA%8E%E6%B3%95%E7%BA%BF%E5%BE%AE%E5%88%86%E7%9A%84%E5%88%86%E5%89%B2/",
  "image": "https://ckyfi9zero.github.io/image/avatar.jpg",
  "datePublished": "2025-08-05T06:29:23.000Z",
  "dateModified": "2025-08-05T07:23:23.970Z",
  "author": [
    {
      "@type": "Person",
      "name": "Fi9zero",
      "url": "https://ckyfi9zero.github.io"
    }
  ]
}</script><link rel="shortcut icon" href="/image/favicon.ico"><link rel="canonical" href="https://ckyfi9zero.github.io/2025/08/05/2025-08-05-%E5%9F%BA%E4%BA%8E%E6%B3%95%E7%BA%BF%E5%BE%AE%E5%88%86%E7%9A%84%E5%88%86%E5%89%B2/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css"><script>
    (() => {
      
    const saveToLocal = {
      set: (key, value, ttl) => {
        if (!ttl) return
        const expiry = Date.now() + ttl * 86400000
        localStorage.setItem(key, JSON.stringify({ value, expiry }))
      },
      get: key => {
        const itemStr = localStorage.getItem(key)
        if (!itemStr) return undefined
        const { value, expiry } = JSON.parse(itemStr)
        if (Date.now() > expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return value
      }
    }

    window.btf = {
      saveToLocal,
      getScript: (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        Object.entries(attr).forEach(([key, val]) => script.setAttribute(key, val))
        script.onload = script.onreadystatechange = () => {
          if (!script.readyState || /loaded|complete/.test(script.readyState)) resolve()
        }
        script.onerror = reject
        document.head.appendChild(script)
      }),
      getCSS: (url, id) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onload = link.onreadystatechange = () => {
          if (!link.readyState || /loaded|complete/.test(link.readyState)) resolve()
        }
        link.onerror = reject
        document.head.appendChild(link)
      }),
      addGlobalFn: (key, fn, name = false, parent = window) => {
        if (!false && key.startsWith('pjax')) return
        const globalFn = parent.globalFn || {}
        globalFn[key] = globalFn[key] || {}
        globalFn[key][name || Object.keys(globalFn[key]).length] = fn
        parent.globalFn = globalFn
      }
    }
  
      
      const activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      const activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }

      btf.activateDarkMode = activateDarkMode
      btf.activateLightMode = activateLightMode

      const theme = saveToLocal.get('theme')
    
          theme === 'dark' ? activateDarkMode() : theme === 'light' ? activateLightMode() : null
        
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        document.documentElement.classList.toggle('hide-aside', asideStatus === 'hide')
      }
    
      
    const detectApple = () => {
      if (/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)) {
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
  
    })()
  </script><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":true,"top_n_per_article":1,"unescape":false,"languages":{"hits_empty":"未找到符合您查询的内容：${query}","hits_stats":"共找到 ${hits} 篇文章"}},
  translate: undefined,
  highlight: {"plugin":"highlight.js","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false,"highlightFullpage":false,"highlightMacStyle":true},
  copy: {
    success: '复制成功',
    error: '复制失败',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'null',
  Snackbar: undefined,
  infinitegrid: {
    js: 'https://cdn.jsdelivr.net/npm/@egjs/infinitegrid/dist/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyloadPlugin: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '基于法线微分的分割',
  isHighlightShrink: false,
  isToc: true,
  pageType: 'post'
}</script><link rel="stylesheet" href="/css/custom.css"><link rel="stylesheet" href="/css/pig.css"><link rel="stylesheet" href="https://npm.elemecdn.com/ethan4116-blog/lib/css/plane_v2.css"><meta name="generator" content="Hexo 7.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img text-center"><img src="/image/avatar.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data text-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">54</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">15</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">8</div></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/booklist/"><i class="fa-fw fas fa-book"></i><span> 书单</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url(/image/per%20page.jpg);"><nav id="nav"><span id="blog-info"><a class="nav-site-title" href="/"><span class="site-name">荒岛</span></a><a class="nav-page-title" href="/"><span class="site-name">基于法线微分的分割</span><span class="site-name"><i class="fa-solid fa-circle-arrow-left"></i><span>  返回首页</span></span></a></span><div id="menus"><div id="search-button"><span class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></span></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/booklist/"><i class="fa-fw fas fa-book"></i><span> 书单</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><span class="site-page"><i class="fas fa-bars fa-fw"></i></span></div></div></nav><div id="post-info"><h1 class="post-title">基于法线微分的分割</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2025-08-05T06:29:23.000Z" title="发表于 2025-08-05 14:29:23">2025-08-05</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2025-08-05T07:23:23.970Z" title="更新于 2025-08-05 15:23:23">2025-08-05</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/PCL/">PCL</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title=""><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">浏览量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="container post-content" id="article-container"><h1 id="基于法线微分的分割"><a href="#基于法线微分的分割" class="headerlink" title="基于法线微分的分割"></a>基于法线微分的分割</h1><h2 id="🔹-1-DoN-Difference-of-Normals-算法原理"><a href="#🔹-1-DoN-Difference-of-Normals-算法原理" class="headerlink" title="🔹 1. DoN (Difference of Normals) 算法原理"></a>🔹 1. <strong>DoN (Difference of Normals) 算法原理</strong></h2><ul>
<li><strong>核心思想</strong>：通过计算<strong>不同尺度</strong>下的法线差异来提取几何特征。</li>
<li><strong>步骤</strong>：<ol>
<li>在<strong>小尺度</strong>下计算法线：反映局部精细细节（如边缘、小物体）。</li>
<li>在<strong>大尺度</strong>下计算法线：反映宏观表面趋势（忽略小细节）。</li>
<li>计算两者之差（DoN）：<code>DoN = Normal_large - Normal_small</code>。</li>
<li>DoN 的模长（或曲率）可以突出<strong>尺度相关</strong>的特征，例如：<ul>
<li><strong>边缘</strong>：小尺度法线变化剧烈，大尺度较平滑，DoN值大。</li>
<li><strong>平面</strong>：两个尺度的法线几乎相同，DoN值接近0。</li>
</ul>
</li>
</ol>
</li>
<li><strong>优势</strong>：对噪声鲁棒，能有效分离不同尺度的几何结构。</li>
</ul>
<hr>
<h2 id="🔹-2-关键参数详解"><a href="#🔹-2-关键参数详解" class="headerlink" title="🔹 2. 关键参数详解"></a>🔹 2. <strong>关键参数详解</strong></h2><table>
<thead>
<tr>
<th>参数</th>
<th>含义</th>
<th>如何设置</th>
</tr>
</thead>
<tbody><tr>
<td><code>scale1</code></td>
<td>小尺度半径</td>
<td>应略大于点云分辨率，能捕捉目标细节。</td>
</tr>
<tr>
<td><code>scale2</code></td>
<td>大尺度半径</td>
<td>应远大于目标物体尺寸，反映背景趋势。必须 <code>scale2 &gt; scale1</code>。</td>
</tr>
<tr>
<td><code>threshold</code></td>
<td>DoN曲率阈值</td>
<td>用于滤除平坦区域。值越小，保留的点越多。可通过打印部分曲率值来调试。</td>
</tr>
<tr>
<td><code>segradius</code></td>
<td>聚类半径容差</td>
<td>用于欧几里得聚类，应略大于目标物体内部点的最大间距。</td>
</tr>
</tbody></table>
<blockquote>
<p>⚠️ <strong>重要</strong>：代码中 <code>scale1</code>, <code>scale2</code>, <code>segradius</code> 是相对于 <code>mean_radius</code> 的倍数，<code>mean_radius</code> 是通过采样点的平均最近邻距离估算的，这是一种自适应设置方法。</p>
</blockquote>
<hr>
<h2 id="🔹-3-核心处理流程"><a href="#🔹-3-核心处理流程" class="headerlink" title="🔹 3. 核心处理流程"></a>🔹 3. <strong>核心处理流程</strong></h2><ol>
<li><strong>加载点云</strong>：读取 <code>.pcd</code> 文件。</li>
<li><strong>估算平均半径</strong>：用于自适应地缩放输入参数。</li>
<li><strong>计算多尺度法线</strong>：<ul>
<li>使用 <code>pcl::NormalEstimationOMP</code> 进行多线程加速。</li>
<li><strong>必须设置 <code>setViewPoint</code></strong>，以确保所有法线方向一致，避免DoN计算错误。</li>
</ul>
</li>
<li><strong>计算DoN特征</strong>：使用 <code>pcl::DifferenceOfNormalsEstimation</code>。</li>
<li><strong>阈值滤波</strong>：使用 <code>pcl::ConditionalRemoval</code> 保留 <code>curvature &gt; threshold</code> 的点。</li>
<li><strong>欧几里得聚类</strong>：对滤波后的点云进行聚类，分离出不同的物体。</li>
<li><strong>可视化与保存</strong>：将结果可视化并保存为PCD文件。</li>
</ol>
<hr>
<h2 id="🔹-4-代码修正与注意事项"><a href="#🔹-4-代码修正与注意事项" class="headerlink" title="🔹 4. 代码修正与注意事项"></a>🔹 4. <strong>代码修正与注意事项</strong></h2><ul>
<li><p><strong><code>ConditionalRemoval</code> 构造函数问题</strong>：</p>
<ul>
<li><strong>错误写法</strong>：<code>pcl::ConditionalRemoval&lt;PointNormal&gt; condrem (range_cond);</code></li>
<li><strong>正确写法</strong>：<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">pcl::ConditionalRemoval&lt;PointNormal&gt; condrem;</span><br><span class="line">condrem.<span class="built_in">setCondition</span>(range_cond);</span><br></pre></td></tr></table></figure></li>
<li>原因：现代PCL版本已废弃直接在构造函数中传入条件的写法。</li>
</ul>
</li>
<li><p><strong><code>mean_radius</code> 计算逻辑小瑕疵</strong>：</p>
<ul>
<li>代码中 <code>total_distance += pointNKNSquaredDistance[1] + pointNKNSquaredDistance[0];</code> 有误。</li>
<li><code>pointNKNSquaredDistance[0]</code> 是点到自身的距离（为0），应只累加 <code>pointNKNSquaredDistance[1]</code>。</li>
</ul>
</li>
<li><p><strong>头文件 <code>#include &lt;pcl/segmentation/impl/extract_clusters.hpp&gt;</code></strong>：</p>
<ul>
<li>通常不需要显式包含 <code>.hpp</code> 实现文件，<code>#include &lt;pcl/segmentation/extract_clusters.h&gt;</code> 已足够。此行可删除。</li>
</ul>
</li>
</ul>
<hr>
<h2 id="🔹-5-可视化技巧"><a href="#🔹-5-可视化技巧" class="headerlink" title="🔹 5. 可视化技巧"></a>🔹 5. <strong>可视化技巧</strong></h2><ul>
<li><strong>双视口对比</strong>：使用 <code>createViewPort</code> 将小尺度和大尺度的法线并排显示，直观对比差异。</li>
<li><strong>字段着色</strong>：使用 <code>PointCloudColorHandlerGenericField</code> 可以根据点云中的任意字段（如 <code>curvature</code>）进行伪彩色渲染。</li>
<li><strong>RGB字段着色</strong>：使用 <code>PointCloudColorHandlerRGBField</code> 可以直接显示点云的RGB颜色。</li>
</ul>
<hr>
<h2 id="🔹-6-典型应用场景"><a href="#🔹-6-典型应用场景" class="headerlink" title="🔹 6. 典型应用场景"></a>🔹 6. <strong>典型应用场景</strong></h2><ul>
<li><strong>城市街景分割</strong>：分离建筑物、车辆、植被、地面。</li>
<li><strong>工业检测</strong>：从复杂背景中提取特定零件。</li>
<li><strong>机器人导航</strong>：识别可通行区域和障碍物。</li>
<li><strong>点云去噪</strong>：滤除平坦的背景区域，保留有几何特征的前景。</li>
</ul>
<hr>
<h2 id="代码实现"><a href="#代码实现" class="headerlink" title="代码实现"></a>代码实现</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @file don_segmentation.cpp</span></span><br><span class="line"><span class="comment"> * Difference of Normals Example for PCL Segmentation Tutorials.</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * @author Yani Ioannou</span></span><br><span class="line"><span class="comment"> * @date 2012-09-24</span></span><br><span class="line"><span class="comment"> * @修改 2015-6-16</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string&gt;</span>                                   <span class="comment">// C++字符串库</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/point_types.h&gt;</span>                       <span class="comment">// PCL 内置点类型</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/io/pcd_io.h&gt;</span>                         <span class="comment">// PCL 点云IO (PCD文件读写)</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/search/organized.h&gt;</span>                  <span class="comment">// 用于有序点云的搜索方法</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/search/kdtree.h&gt;</span>                     <span class="comment">// 用于无序点云的KDTree搜索</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/features/normal_3d_omp.h&gt;</span>            <span class="comment">// 多线程法线估计</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/filters/conditional_removal.h&gt;</span>       <span class="comment">// 条件滤波器</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/segmentation/extract_clusters.h&gt;</span>     <span class="comment">// 聚类提取</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/segmentation/impl/extract_clusters.hpp&gt;</span> <span class="comment">// 聚类提取的实现文件 (通常不需要显式包含)</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/features/don.h&gt;</span>                      <span class="comment">// Difference of Normals (DoN) 特征计算</span></span></span><br><span class="line"><span class="comment">// for visualization</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/visualization/pcl_visualizer.h&gt;</span>      <span class="comment">// PCL 可视化工具</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> pcl;                                <span class="comment">// 使用PCL命名空间</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;                                <span class="comment">// 使用std命名空间</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 自定义函数：根据聚类结果为点云生成彩色版本</span></span><br><span class="line">pcl::PointCloud&lt;pcl::PointXYZRGB&gt;::<span class="function">Ptr <span class="title">getColoredCloud</span> <span class="params">(</span></span></span><br><span class="line"><span class="params"><span class="function">    pcl::PointCloud&lt;pcl::PointXYZ&gt;::Ptr input_,           <span class="comment">// 输入的XYZ点云</span></span></span></span><br><span class="line"><span class="params"><span class="function">    std::vector &lt;pcl::PointIndices&gt; clusters_,             <span class="comment">// 聚类索引</span></span></span></span><br><span class="line"><span class="params"><span class="function">    <span class="type">float</span> r,<span class="type">float</span> g,<span class="type">float</span> b)</span>                               <span class="comment">// 默认颜色</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">  pcl::PointCloud&lt;pcl::PointXYZRGB&gt;::Ptr colored_cloud;   <span class="comment">// 声明一个共享指针用于返回</span></span><br><span class="line">  <span class="keyword">if</span> (!clusters_.<span class="built_in">empty</span> ())                                <span class="comment">// 如果聚类结果不为空</span></span><br><span class="line">  &#123;</span><br><span class="line">    <span class="comment">// 创建新的共享指针并初始化</span></span><br><span class="line">    colored_cloud = (<span class="keyword">new</span> pcl::PointCloud&lt;pcl::PointXYZRGB&gt;)-&gt;<span class="built_in">makeShared</span> ();</span><br><span class="line">    <span class="built_in">srand</span> (<span class="built_in">static_cast</span>&lt;<span class="type">unsigned</span> <span class="type">int</span>&gt; (<span class="built_in">time</span> (<span class="number">0</span>)));         <span class="comment">// 初始化随机数种子</span></span><br><span class="line">    std::vector&lt;<span class="type">unsigned</span> <span class="type">char</span>&gt; colors;                    <span class="comment">// 存储每个聚类的随机颜色</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">size_t</span> i_segment = <span class="number">0</span>; i_segment &lt; clusters_.<span class="built_in">size</span> (); i_segment++)</span><br><span class="line">    &#123;</span><br><span class="line">      colors.<span class="built_in">push_back</span> (<span class="built_in">static_cast</span>&lt;<span class="type">unsigned</span> <span class="type">char</span>&gt; (<span class="built_in">rand</span> () % <span class="number">256</span>)); <span class="comment">// R</span></span><br><span class="line">      colors.<span class="built_in">push_back</span> (<span class="built_in">static_cast</span>&lt;<span class="type">unsigned</span> <span class="type">char</span>&gt; (<span class="built_in">rand</span> () % <span class="number">256</span>)); <span class="comment">// G</span></span><br><span class="line">      colors.<span class="built_in">push_back</span> (<span class="built_in">static_cast</span>&lt;<span class="type">unsigned</span> <span class="type">char</span>&gt; (<span class="built_in">rand</span> () % <span class="number">256</span>)); <span class="comment">// B</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 设置新点云的元数据</span></span><br><span class="line">    colored_cloud-&gt;width = input_-&gt;width;</span><br><span class="line">    colored_cloud-&gt;height = input_-&gt;height;</span><br><span class="line">    colored_cloud-&gt;is_dense = input_-&gt;is_dense;</span><br><span class="line">    <span class="comment">// 为所有点设置默认颜色并复制坐标</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">size_t</span> i_point = <span class="number">0</span>; i_point &lt; input_-&gt;points.<span class="built_in">size</span> (); i_point++)</span><br><span class="line">    &#123;</span><br><span class="line">      pcl::PointXYZRGB point;</span><br><span class="line">      point.x = *(input_-&gt;points[i_point].data);            <span class="comment">// 获取x坐标</span></span><br><span class="line">      point.y = *(input_-&gt;points[i_point].data + <span class="number">1</span>);        <span class="comment">// 获取y坐标</span></span><br><span class="line">      point.z = *(input_-&gt;points[i_point].data + <span class="number">2</span>);        <span class="comment">// 获取z坐标</span></span><br><span class="line">      point.r = r; point.g = g; point.b = b;                <span class="comment">// 设置默认颜色</span></span><br><span class="line">      colored_cloud-&gt;points.<span class="built_in">push_back</span> (point);              <span class="comment">// 添加到点云</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 为每个聚类中的点赋予不同的随机颜色</span></span><br><span class="line">    std::vector&lt; pcl::PointIndices &gt;::iterator i_segment;</span><br><span class="line">    <span class="type">int</span> next_color = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span> (i_segment = clusters_.<span class="built_in">begin</span> (); i_segment != clusters_.<span class="built_in">end</span> (); i_segment++)</span><br><span class="line">    &#123;</span><br><span class="line">      std::vector&lt;<span class="type">int</span>&gt;::iterator i_point;</span><br><span class="line">      <span class="keyword">for</span> (i_point = i_segment-&gt;indices.<span class="built_in">begin</span> (); i_point != i_segment-&gt;indices.<span class="built_in">end</span> (); i_point++)</span><br><span class="line">      &#123;</span><br><span class="line">        <span class="type">int</span> index = *i_point;                               <span class="comment">// 获取点的索引</span></span><br><span class="line">        <span class="comment">// 根据聚类ID设置RGB颜色</span></span><br><span class="line">        colored_cloud-&gt;points[index].r = colors[<span class="number">3</span> * next_color];</span><br><span class="line">        colored_cloud-&gt;points[index].g = colors[<span class="number">3</span> * next_color + <span class="number">1</span>];</span><br><span class="line">        colored_cloud-&gt;points[index].b = colors[<span class="number">3</span> * next_color + <span class="number">2</span>];</span><br><span class="line">      &#125;</span><br><span class="line">      next_color++;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> (colored_cloud);                                   <span class="comment">// 返回着色后的点云</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span> <span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span>                          <span class="comment">// 主函数</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="type">int</span> VISUAL=<span class="number">1</span>,SAVE=<span class="number">0</span>; <span class="comment">// 0:不显示, 1:显示每一步, 2:只显示最终结果</span></span><br><span class="line">  <span class="comment">///The smallest scale to use in the DoN filter.</span></span><br><span class="line">  <span class="type">double</span> scale1,mean_radius;                                <span class="comment">// 最小尺度、点云平均半径</span></span><br><span class="line">  <span class="comment">///The largest scale to use in the DoN filter.</span></span><br><span class="line">  <span class="type">double</span> scale2;                                            <span class="comment">// 最大尺度</span></span><br><span class="line">  <span class="comment">///The minimum DoN magnitude to threshold by</span></span><br><span class="line">  <span class="type">double</span> threshold;                                         <span class="comment">// DoN曲率阈值</span></span><br><span class="line">  <span class="comment">///segment scene into clusters with given distance tolerance using euclidean clustering</span></span><br><span class="line">  <span class="type">double</span> segradius;                                         <span class="comment">// 欧几里得聚类的半径容差</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">// 检查命令行参数数量</span></span><br><span class="line">  <span class="keyword">if</span> (argc &lt; <span class="number">6</span>)</span><br><span class="line">  &#123;</span><br><span class="line">    <span class="comment">// 打印使用说明</span></span><br><span class="line">    cerr &lt;&lt; <span class="string">&quot;usage: &quot;</span> &lt;&lt; argv[<span class="number">0</span>] &lt;&lt; <span class="string">&quot; inputfile smallscale(5) largescale(10) threshold(0.1) segradius(1.5) VISUAL(1) SAVE(0)&quot;</span> &lt;&lt; endl;</span><br><span class="line">	cerr &lt;&lt; <span class="string">&quot;usage: &quot;</span>&lt;&lt;<span class="string">&quot;smallscale largescale  segradius :multiple with mean radius of point cloud &quot;</span>&lt;&lt; endl;</span><br><span class="line">    <span class="built_in">exit</span> (EXIT_FAILURE);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">/// the file to read from.</span></span><br><span class="line">  string infile = argv[<span class="number">1</span>];                                  <span class="comment">// 输入文件名</span></span><br><span class="line">  <span class="comment">/// small scale</span></span><br><span class="line">  <span class="built_in">istringstream</span> (argv[<span class="number">2</span>]) &gt;&gt; scale1;                        <span class="comment">// 解析最小尺度</span></span><br><span class="line">  <span class="comment">/// large scale</span></span><br><span class="line">  <span class="built_in">istringstream</span> (argv[<span class="number">3</span>]) &gt;&gt; scale2;                        <span class="comment">// 解析最大尺度</span></span><br><span class="line">  <span class="built_in">istringstream</span> (argv[<span class="number">4</span>]) &gt;&gt; threshold;   <span class="comment">// threshold for DoN magnitude</span></span><br><span class="line">  <span class="built_in">istringstream</span> (argv[<span class="number">5</span>]) &gt;&gt; segradius;   <span class="comment">// threshold for radius segmentation</span></span><br><span class="line">  <span class="built_in">istringstream</span> (argv[<span class="number">6</span>]) &gt;&gt; VISUAL;                        <span class="comment">// 解析可视化级别</span></span><br><span class="line">   <span class="built_in">istringstream</span> (argv[<span class="number">7</span>]) &gt;&gt; SAVE;                         <span class="comment">// 解析是否保存</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">// Load cloud in blob format</span></span><br><span class="line">  pcl::PointCloud&lt;PointXYZRGB&gt;::<span class="function">Ptr <span class="title">cloud</span> <span class="params">(<span class="keyword">new</span> pcl::PointCloud&lt;PointXYZRGB&gt;)</span></span>; <span class="comment">// 创建输入点云指针</span></span><br><span class="line">  pcl::io::<span class="built_in">loadPCDFile</span> (infile.<span class="built_in">c_str</span> (), *cloud);           <span class="comment">// 加载PCD文件</span></span><br><span class="line"></span><br><span class="line">   <span class="comment">// Create a search tree, use KDTreee for non-organized data.</span></span><br><span class="line">  pcl::search::Search&lt;PointXYZRGB&gt;::Ptr tree;               <span class="comment">// 创建搜索树指针</span></span><br><span class="line">  <span class="keyword">if</span> (cloud-&gt;<span class="built_in">isOrganized</span> ())                                <span class="comment">// 如果点云是有序的 (如深度相机)</span></span><br><span class="line">  &#123;</span><br><span class="line">    tree.<span class="built_in">reset</span> (<span class="keyword">new</span> pcl::search::<span class="built_in">OrganizedNeighbor</span>&lt;PointXYZRGB&gt; ()); <span class="comment">// 使用OrganizedNeighbor</span></span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">else</span>                                                      <span class="comment">// 如果点云是无序的</span></span><br><span class="line">  &#123;</span><br><span class="line">    tree.<span class="built_in">reset</span> (<span class="keyword">new</span> pcl::search::<span class="built_in">KdTree</span>&lt;PointXYZRGB&gt; (<span class="literal">false</span>)); <span class="comment">// 使用KdTree</span></span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">// Set the input pointcloud for the search tree</span></span><br><span class="line">  tree-&gt;<span class="built_in">setInputCloud</span> (cloud);                              <span class="comment">// 为搜索树设置输入点云</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">//caculate the mean radius of cloud and mutilply with corresponding input</span></span><br><span class="line">  &#123;</span><br><span class="line">	  <span class="type">int</span> size_cloud=cloud-&gt;<span class="built_in">size</span>();                         <span class="comment">// 获取点云大小</span></span><br><span class="line">	  <span class="type">int</span> step=size_cloud/<span class="number">10</span>;                               <span class="comment">// 计算采样步长 (取10个点)</span></span><br><span class="line">	  <span class="type">double</span> total_distance=<span class="number">0</span>;</span><br><span class="line">	  <span class="type">int</span> i,j=<span class="number">1</span>;</span><br><span class="line">	  <span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;size_cloud;i+=step,j++)                     <span class="comment">// 遍历采样点</span></span><br><span class="line">	  &#123;</span><br><span class="line">		  <span class="function">std::vector&lt;<span class="type">int</span>&gt; <span class="title">pointIdxNKNSearch</span><span class="params">(<span class="number">2</span>)</span></span>;            <span class="comment">// 存储最近邻点的索引</span></span><br><span class="line">		  <span class="function">std::vector&lt;<span class="type">float</span>&gt; <span class="title">pointNKNSquaredDistance</span><span class="params">(<span class="number">2</span>)</span></span>;    <span class="comment">// 存储最近邻点的距离平方</span></span><br><span class="line">		  <span class="comment">// 搜索最近的2个点 (自身和最近邻)</span></span><br><span class="line">		  tree-&gt;<span class="built_in">nearestKSearch</span>(cloud-&gt;points[i],<span class="number">2</span>,pointIdxNKNSearch,pointNKNSquaredDistance);</span><br><span class="line">		  <span class="comment">// 累加距离平方和 (这里逻辑有误，应只加[1])</span></span><br><span class="line">		  total_distance+=pointNKNSquaredDistance[<span class="number">1</span>]+pointNKNSquaredDistance[<span class="number">0</span>];</span><br><span class="line">	  &#125;</span><br><span class="line">	  mean_radius=<span class="built_in">sqrt</span>((total_distance/j));                 <span class="comment">// 计算平均距离作为mean_radius</span></span><br><span class="line">	  cout&lt;&lt;<span class="string">&quot;mean radius of cloud is£º &quot;</span>&lt;&lt;mean_radius&lt;&lt;endl;</span><br><span class="line">	  scale1*=mean_radius;                                  <span class="comment">// 将输入的尺度乘以mean_radius</span></span><br><span class="line">	  scale2*=mean_radius;</span><br><span class="line">	  segradius*=mean_radius;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 检查尺度参数</span></span><br><span class="line">  <span class="keyword">if</span> (scale1 &gt;= scale2)</span><br><span class="line">  &#123;</span><br><span class="line">    cerr &lt;&lt; <span class="string">&quot;Error: Large scale must be &gt; small scale!&quot;</span> &lt;&lt; endl;</span><br><span class="line">    <span class="built_in">exit</span> (EXIT_FAILURE);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// Compute normals using both small and large scales at each point</span></span><br><span class="line">  pcl::NormalEstimationOMP&lt;PointXYZRGB, PointNormal&gt; ne;    <span class="comment">// 创建多线程法线估计对象</span></span><br><span class="line">  ne.<span class="built_in">setInputCloud</span> (cloud);                                 <span class="comment">// 设置输入点云</span></span><br><span class="line">  ne.<span class="built_in">setSearchMethod</span> (tree);                                <span class="comment">// 设置搜索方法</span></span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * <span class="doctag">NOTE:</span> setting viewpoint is very important, so that we can ensure</span></span><br><span class="line"><span class="comment">   * normals are all pointed in the same direction!</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="comment">// 设置视点，确保所有法线方向一致 (指向无穷远)</span></span><br><span class="line">  ne.<span class="built_in">setViewPoint</span> (std::numeric_limits&lt;<span class="type">float</span>&gt;::<span class="built_in">max</span> (), std::numeric_limits&lt;<span class="type">float</span>&gt;::<span class="built_in">max</span> (), std::numeric_limits&lt;<span class="type">float</span>&gt;::<span class="built_in">max</span> ());</span><br><span class="line"></span><br><span class="line">  <span class="comment">// calculate normals with the small scale</span></span><br><span class="line">  cout &lt;&lt; <span class="string">&quot;Calculating normals for scale...&quot;</span> &lt;&lt; scale1 &lt;&lt; endl;</span><br><span class="line">  pcl::PointCloud&lt;PointNormal&gt;::<span class="function">Ptr <span class="title">normals_small_scale</span> <span class="params">(<span class="keyword">new</span> pcl::PointCloud&lt;PointNormal&gt;)</span></span>; <span class="comment">// 小尺度法线</span></span><br><span class="line">  ne.<span class="built_in">setRadiusSearch</span> (scale1);                              <span class="comment">// 设置搜索半径为小尺度</span></span><br><span class="line">  ne.<span class="built_in">compute</span> (*normals_small_scale);                        <span class="comment">// 计算法线</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">// calculate normals with the large scale</span></span><br><span class="line">  cout &lt;&lt; <span class="string">&quot;Calculating normals for scale...&quot;</span> &lt;&lt; scale2 &lt;&lt; endl;</span><br><span class="line">  pcl::PointCloud&lt;PointNormal&gt;::<span class="function">Ptr <span class="title">normals_large_scale</span> <span class="params">(<span class="keyword">new</span> pcl::PointCloud&lt;PointNormal&gt;)</span></span>; <span class="comment">// 大尺度法线</span></span><br><span class="line">  ne.<span class="built_in">setRadiusSearch</span> (scale2);                              <span class="comment">// 设置搜索半径为大尺度</span></span><br><span class="line">  ne.<span class="built_in">compute</span> (*normals_large_scale);                        <span class="comment">// 计算法线</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">//visualize the normals</span></span><br><span class="line">  <span class="keyword">if</span>(VISUAL==<span class="number">1</span>)                                             <span class="comment">// 如果需要可视化</span></span><br><span class="line">  &#123;</span><br><span class="line">	  cout &lt;&lt; <span class="string">&quot;click q key to quit the visualizer and continue£¡£¡&quot;</span> &lt;&lt; endl;</span><br><span class="line">	  <span class="comment">// 创建一个共享指针的可视化器</span></span><br><span class="line">	  <span class="function">boost::shared_ptr&lt;pcl::visualization::PCLVisualizer&gt; <span class="title">MView</span> <span class="params">(<span class="keyword">new</span> pcl::visualization::PCLVisualizer (<span class="string">&quot;Showing normals with different scale&quot;</span>))</span></span>; </span><br><span class="line">	  <span class="comment">// 创建绿色点云处理器</span></span><br><span class="line">	  pcl::<span class="function">visualization::PointCloudColorHandlerCustom&lt;pcl::PointXYZRGB&gt; <span class="title">green</span> <span class="params">(cloud, <span class="number">0</span>,<span class="number">255</span>,<span class="number">0</span>)</span></span>; </span><br><span class="line">	  <span class="function"><span class="type">int</span> <span class="title">v1</span><span class="params">(<span class="number">0</span>)</span>,<span class="title">v2</span><span class="params">(<span class="number">0</span>)</span></span>; </span><br><span class="line">	  MView-&gt;<span class="built_in">createViewPort</span> (<span class="number">0.0</span>, <span class="number">0.0</span>, <span class="number">0.5</span>, <span class="number">1.0</span>, v1);       <span class="comment">// 创建左视口</span></span><br><span class="line">	  MView-&gt;<span class="built_in">createViewPort</span> (<span class="number">0.5</span>, <span class="number">0.0</span>, <span class="number">1.0</span>, <span class="number">1.0</span>, v2);       <span class="comment">// 创建右视口</span></span><br><span class="line">	  MView-&gt;<span class="built_in">setBackgroundColor</span> (<span class="number">1</span>,<span class="number">1</span>,<span class="number">1</span>);                    <span class="comment">// 设置背景为白色</span></span><br><span class="line">	  MView-&gt;<span class="built_in">addPointCloud</span> (cloud, green, <span class="string">&quot;small_scale&quot;</span>, v1); <span class="comment">// 在左视口添加点云</span></span><br><span class="line">	  MView-&gt;<span class="built_in">addPointCloud</span> (cloud, green, <span class="string">&quot;large_scale&quot;</span>, v2); <span class="comment">// 在右视口添加点云</span></span><br><span class="line">	  <span class="comment">// 在左视口添加小尺度法线</span></span><br><span class="line">	  MView-&gt;<span class="built_in">addPointCloudNormals</span>&lt;pcl::PointXYZRGB,pcl::PointNormal&gt;(cloud,normals_small_scale,<span class="number">100</span>,mean_radius*<span class="number">10</span>,<span class="string">&quot;small_scale_normal&quot;</span>);</span><br><span class="line">	  <span class="comment">// 在右视口添加大尺度法线</span></span><br><span class="line">	  MView-&gt;<span class="built_in">addPointCloudNormals</span>&lt;pcl::PointXYZRGB,pcl::PointNormal&gt;(cloud,normals_large_scale,<span class="number">100</span>,mean_radius*<span class="number">10</span>,<span class="string">&quot;large_scale_normal&quot;</span>);</span><br><span class="line">	  MView-&gt;<span class="built_in">setPointCloudRenderingProperties</span>(pcl::visualization::PCL_VISUALIZER_POINT_SIZE,<span class="number">3</span>,<span class="string">&quot;small_scale&quot;</span>,v1);</span><br><span class="line">	  MView-&gt;<span class="built_in">setPointCloudRenderingProperties</span>(pcl::visualization::PCL_VISUALIZER_OPACITY,<span class="number">0.5</span>,<span class="string">&quot;small_scale&quot;</span>,v1);</span><br><span class="line">	  MView-&gt;<span class="built_in">setPointCloudRenderingProperties</span>(pcl::visualization::PCL_VISUALIZER_POINT_SIZE,<span class="number">3</span>,<span class="string">&quot;large_scale&quot;</span>,v1);</span><br><span class="line">	  MView-&gt;<span class="built_in">setPointCloudRenderingProperties</span>(pcl::visualization::PCL_VISUALIZER_OPACITY,<span class="number">0.5</span>,<span class="string">&quot;large_scale&quot;</span>,v1);</span><br><span class="line">	  MView-&gt;<span class="built_in">spin</span>();                                        <span class="comment">// 启动可视化器</span></span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// Create output cloud for DoN results</span></span><br><span class="line">  PointCloud&lt;PointNormal&gt;::<span class="function">Ptr <span class="title">doncloud</span> <span class="params">(<span class="keyword">new</span> pcl::PointCloud&lt;PointNormal&gt;)</span></span>; <span class="comment">// 创建DoN输出点云</span></span><br><span class="line">  <span class="comment">// 将输入点云的XYZ坐标复制到DoN点云</span></span><br><span class="line">  <span class="built_in">copyPointCloud</span>&lt;PointXYZRGB, PointNormal&gt;(*cloud, *doncloud);</span><br><span class="line">  cout &lt;&lt; <span class="string">&quot;Calculating DoN... &quot;</span> &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// Create DoN operator</span></span><br><span class="line">  pcl::DifferenceOfNormalsEstimation&lt;PointXYZRGB, PointNormal, PointNormal&gt; don; <span class="comment">// 创建DoN对象</span></span><br><span class="line">  don.<span class="built_in">setInputCloud</span> (cloud);                              <span class="comment">// 设置输入点云</span></span><br><span class="line">  don.<span class="built_in">setNormalScaleLarge</span> (normals_large_scale);          <span class="comment">// 设置大尺度法线</span></span><br><span class="line">  don.<span class="built_in">setNormalScaleSmall</span> (normals_small_scale);          <span class="comment">// 设置小尺度法线</span></span><br><span class="line">  <span class="keyword">if</span> (!don.<span class="built_in">initCompute</span> ())                                <span class="comment">// 初始化计算</span></span><br><span class="line">  &#123;</span><br><span class="line">    std::cerr &lt;&lt; <span class="string">&quot;Error: Could not intialize DoN feature operator&quot;</span> &lt;&lt; std::endl;</span><br><span class="line">    <span class="built_in">exit</span> (EXIT_FAILURE);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">// Compute DoN</span></span><br><span class="line">  don.<span class="built_in">computeFeature</span> (*doncloud);                         <span class="comment">// 计算DoN特征，结果存储在curvature字段</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">//print some differencense of curvature</span></span><br><span class="line">  &#123;</span><br><span class="line">	   cout &lt;&lt; <span class="string">&quot;You may have some sense about the input threshold£¨curvature£© next time for your data&quot;</span> &lt;&lt; endl;</span><br><span class="line">	  <span class="type">int</span> size_cloud=doncloud-&gt;<span class="built_in">size</span>();</span><br><span class="line">	  <span class="type">int</span> step=size_cloud/<span class="number">10</span>;</span><br><span class="line">	  <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">0</span>;i&lt;size_cloud;i+=step)                    <span class="comment">// 打印部分点的曲率值，用于调试阈值</span></span><br><span class="line">      cout &lt;&lt; <span class="string">&quot; &quot;</span>&lt;&lt;doncloud-&gt;points[i].curvature&lt;&lt;<span class="string">&quot; &quot;</span>&lt;&lt; endl;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">   <span class="comment">//show the differences of curvature with both large and small scale </span></span><br><span class="line">  <span class="keyword">if</span>(VISUAL==<span class="number">1</span>)</span><br><span class="line">  &#123;</span><br><span class="line">	  cout &lt;&lt; <span class="string">&quot;click q key to quit the visualizer and continue£¡£¡&quot;</span> &lt;&lt; endl;</span><br><span class="line">	  <span class="comment">// 可视化DoN结果（曲率）</span></span><br><span class="line">	  <span class="function">boost::shared_ptr&lt;pcl::visualization::PCLVisualizer&gt; <span class="title">MView</span> <span class="params">(<span class="keyword">new</span> pcl::visualization::PCLVisualizer (<span class="string">&quot;Showing the difference of curvature of two scale&quot;</span>))</span></span>; </span><br><span class="line">	  <span class="comment">// 使用曲率字段进行着色</span></span><br><span class="line">	  pcl::<span class="function">visualization::PointCloudColorHandlerGenericField&lt;pcl::PointNormal&gt; <span class="title">handler_k</span><span class="params">(doncloud,<span class="string">&quot;curvature&quot;</span>)</span></span>; </span><br><span class="line">	  MView-&gt;<span class="built_in">setBackgroundColor</span> (<span class="number">1</span>,<span class="number">1</span>,<span class="number">1</span>); </span><br><span class="line">	  MView-&gt;<span class="built_in">addPointCloud</span> (doncloud, handler_k); </span><br><span class="line">	  MView-&gt;<span class="built_in">setPointCloudRenderingProperties</span>(pcl::visualization::PCL_VISUALIZER_POINT_SIZE,<span class="number">3</span>);</span><br><span class="line">	  MView-&gt;<span class="built_in">setPointCloudRenderingProperties</span>(pcl::visualization::PCL_VISUALIZER_OPACITY,<span class="number">0.5</span>);</span><br><span class="line">	  MView-&gt;<span class="built_in">spin</span>();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// Save DoN features</span></span><br><span class="line">  pcl::PCDWriter writer;                                    <span class="comment">// 创建PCD写入器</span></span><br><span class="line">   <span class="keyword">if</span>(SAVE==<span class="number">1</span>) writer.<span class="built_in">write</span>&lt;pcl::PointNormal&gt; (<span class="string">&quot;don.pcd&quot;</span>, *doncloud, <span class="literal">false</span>); <span class="comment">// 保存DoN结果</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">// Filter by magnitude</span></span><br><span class="line">  cout &lt;&lt; <span class="string">&quot;Filtering out DoN mag &lt;= &quot;</span> &lt;&lt; threshold &lt;&lt; <span class="string">&quot;...&quot;</span> &lt;&lt; endl;</span><br><span class="line">  <span class="comment">// Build the condition for filtering</span></span><br><span class="line">  <span class="comment">// 创建一个“或”条件，用于过滤</span></span><br><span class="line">  pcl::ConditionOr&lt;PointNormal&gt;::<span class="function">Ptr <span class="title">range_cond</span> <span class="params">(<span class="keyword">new</span> pcl::ConditionOr&lt;PointNormal&gt; ())</span></span>;</span><br><span class="line">  <span class="comment">// 添加一个比较：curvature &gt; threshold</span></span><br><span class="line">  range_cond-&gt;<span class="built_in">addComparison</span> (pcl::FieldComparison&lt;PointNormal&gt;::<span class="built_in">ConstPtr</span> (</span><br><span class="line">                               <span class="keyword">new</span> pcl::<span class="built_in">FieldComparison</span>&lt;PointNormal&gt; (<span class="string">&quot;curvature&quot;</span>, pcl::ComparisonOps::GT, threshold))</span><br><span class="line">                             );</span><br><span class="line"></span><br><span class="line">  <span class="comment">// Build the filter</span></span><br><span class="line">  <span class="comment">// 修正：使用新式API</span></span><br><span class="line">  pcl::ConditionalRemoval&lt;PointNormal&gt; condrem;             <span class="comment">// 创建条件滤波器对象</span></span><br><span class="line">  condrem.<span class="built_in">setCondition</span>(range_cond);                         <span class="comment">// 设置过滤条件</span></span><br><span class="line">  condrem.<span class="built_in">setInputCloud</span>(doncloud);                          <span class="comment">// 设置输入</span></span><br><span class="line">  pcl::PointCloud&lt;PointNormal&gt;::<span class="function">Ptr <span class="title">doncloud_filtered</span> <span class="params">(<span class="keyword">new</span> pcl::PointCloud&lt;PointNormal&gt;)</span></span>;</span><br><span class="line">  condrem.<span class="built_in">filter</span>(*doncloud_filtered);                       <span class="comment">// 执行滤波</span></span><br><span class="line">  doncloud = doncloud_filtered;                             <span class="comment">// 更新点云指针</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">// Save filtered output</span></span><br><span class="line">  std::cout &lt;&lt; <span class="string">&quot;Filtered Pointcloud: &quot;</span> &lt;&lt; doncloud-&gt;points.<span class="built_in">size</span> () &lt;&lt; <span class="string">&quot; data points.&quot;</span> &lt;&lt; std::endl;</span><br><span class="line">  <span class="keyword">if</span>(SAVE==<span class="number">1</span>)writer.<span class="built_in">write</span>&lt;pcl::PointNormal&gt; (<span class="string">&quot;don_filtered.pcd&quot;</span>, *doncloud, <span class="literal">false</span>); </span><br><span class="line"></span><br><span class="line">   <span class="comment">//show the results of keeping relative small curvature points </span></span><br><span class="line">  <span class="keyword">if</span>(VISUAL==<span class="number">1</span>)</span><br><span class="line">  &#123;</span><br><span class="line">	  cout &lt;&lt; <span class="string">&quot;click q key to quit the visualizer and continue&quot;</span> &lt;&lt; endl;</span><br><span class="line">	  <span class="comment">// 可视化滤波后的结果</span></span><br><span class="line">	  <span class="function">boost::shared_ptr&lt;pcl::visualization::PCLVisualizer&gt; <span class="title">MView</span> <span class="params">(<span class="keyword">new</span> pcl::visualization::PCLVisualizer (<span class="string">&quot;Showing the results of keeping relative small curvature points&quot;</span>))</span></span>; </span><br><span class="line">	  pcl::<span class="function">visualization::PointCloudColorHandlerGenericField&lt;pcl::PointNormal&gt; <span class="title">handler_k</span><span class="params">(doncloud,<span class="string">&quot;curvature&quot;</span>)</span></span>; </span><br><span class="line">	  MView-&gt;<span class="built_in">setBackgroundColor</span> (<span class="number">1</span>,<span class="number">1</span>,<span class="number">1</span>); </span><br><span class="line">	  MView-&gt;<span class="built_in">addPointCloud</span> (doncloud, handler_k); </span><br><span class="line">	  MView-&gt;<span class="built_in">setPointCloudRenderingProperties</span>(pcl::visualization::PCL_VISUALIZER_POINT_SIZE,<span class="number">3</span>);</span><br><span class="line">	  MView-&gt;<span class="built_in">setPointCloudRenderingProperties</span>(pcl::visualization::PCL_VISUALIZER_OPACITY,<span class="number">0.5</span>);</span><br><span class="line">	  MView-&gt;<span class="built_in">spin</span>();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// Filter by magnitude</span></span><br><span class="line">  cout &lt;&lt; <span class="string">&quot;Clustering using EuclideanClusterExtraction with tolerance &lt;= &quot;</span> &lt;&lt; segradius &lt;&lt; <span class="string">&quot;...&quot;</span> &lt;&lt; endl;</span><br><span class="line">  <span class="comment">// 为聚类创建KDTree</span></span><br><span class="line">  pcl::search::KdTree&lt;PointNormal&gt;::<span class="function">Ptr <span class="title">segtree</span> <span class="params">(<span class="keyword">new</span> pcl::search::KdTree&lt;PointNormal&gt;)</span></span>;</span><br><span class="line">  segtree-&gt;<span class="built_in">setInputCloud</span> (doncloud);</span><br><span class="line">  std::vector&lt;pcl::PointIndices&gt; cluster_indices;           <span class="comment">// 存储聚类结果</span></span><br><span class="line">  pcl::EuclideanClusterExtraction&lt;PointNormal&gt; ec;          <span class="comment">// 创建欧几里得聚类对象</span></span><br><span class="line">  ec.<span class="built_in">setClusterTolerance</span> (segradius);                       <span class="comment">// 设置聚类容差</span></span><br><span class="line">  ec.<span class="built_in">setMinClusterSize</span> (<span class="number">50</span>);                                <span class="comment">// 设置最小聚类点数</span></span><br><span class="line">  ec.<span class="built_in">setMaxClusterSize</span> (<span class="number">100000</span>);                            <span class="comment">// 设置最大聚类点数</span></span><br><span class="line">  ec.<span class="built_in">setSearchMethod</span> (segtree);                             <span class="comment">// 设置搜索方法</span></span><br><span class="line">  ec.<span class="built_in">setInputCloud</span> (doncloud);                              <span class="comment">// 设置输入点云</span></span><br><span class="line">  ec.<span class="built_in">extract</span> (cluster_indices);                             <span class="comment">// 执行聚类</span></span><br><span class="line"></span><br><span class="line">  <span class="keyword">if</span>(VISUAL==<span class="number">1</span>)</span><br><span class="line">  &#123;<span class="comment">//visualize the clustering results</span></span><br><span class="line">	  <span class="comment">// 将PointNormal转换为PointXYZ用于着色</span></span><br><span class="line">	  pcl::PointCloud &lt;pcl::PointXYZ&gt;::<span class="function">Ptr <span class="title">tmp_xyz</span><span class="params">(<span class="keyword">new</span> pcl::PointCloud&lt;pcl::PointXYZ&gt;)</span></span>;</span><br><span class="line">	  <span class="built_in">copyPointCloud</span>&lt;pcl::PointNormal,pcl::PointXYZ&gt;(*doncloud,*tmp_xyz);</span><br><span class="line">	  <span class="comment">// 调用自定义函数生成彩色点云</span></span><br><span class="line">	  pcl::PointCloud &lt;pcl::PointXYZRGB&gt;::Ptr colored_cloud = <span class="built_in">getColoredCloud</span> (tmp_xyz,cluster_indices,<span class="number">0</span>,<span class="number">255</span>,<span class="number">0</span>);</span><br><span class="line">	  cout &lt;&lt; <span class="string">&quot;click q key to quit the visualizer and continue&quot;</span> &lt;&lt; endl;</span><br><span class="line">	  <span class="function">boost::shared_ptr&lt;pcl::visualization::PCLVisualizer&gt; <span class="title">MView</span> <span class="params">(<span class="keyword">new</span> pcl::visualization::PCLVisualizer (<span class="string">&quot;visualize the clustering results&quot;</span>))</span></span>; </span><br><span class="line">	  <span class="comment">// 使用RGB字段进行着色</span></span><br><span class="line">	 pcl::<span class="function">visualization::PointCloudColorHandlerRGBField&lt;pcl::PointXYZRGB&gt; <span class="title">rgbps</span><span class="params">(colored_cloud)</span></span>; </span><br><span class="line">	  MView-&gt;<span class="built_in">setBackgroundColor</span> (<span class="number">1</span>,<span class="number">1</span>,<span class="number">1</span>); </span><br><span class="line">	  MView-&gt;<span class="built_in">addPointCloud</span> (colored_cloud, rgbps); </span><br><span class="line">	  MView-&gt;<span class="built_in">setPointCloudRenderingProperties</span>(pcl::visualization::PCL_VISUALIZER_POINT_SIZE,<span class="number">3</span>);</span><br><span class="line">	  MView-&gt;<span class="built_in">setPointCloudRenderingProperties</span>(pcl::visualization::PCL_VISUALIZER_OPACITY,<span class="number">0.5</span>);</span><br><span class="line">	  MView-&gt;<span class="built_in">spin</span>();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">if</span>(SAVE==<span class="number">1</span>)</span><br><span class="line">  &#123;</span><br><span class="line">	  <span class="type">int</span> j = <span class="number">0</span>;</span><br><span class="line">	  <span class="comment">// 遍历每个聚类</span></span><br><span class="line">	  <span class="keyword">for</span> (std::vector&lt;pcl::PointIndices&gt;::const_iterator it = cluster_indices.<span class="built_in">begin</span> (); it != cluster_indices.<span class="built_in">end</span> (); ++it, j++)</span><br><span class="line">	  &#123;</span><br><span class="line">		  pcl::PointCloud&lt;PointNormal&gt;::<span class="function">Ptr <span class="title">cloud_cluster_don</span> <span class="params">(<span class="keyword">new</span> pcl::PointCloud&lt;PointNormal&gt;)</span></span>;</span><br><span class="line">		  <span class="comment">// 提取该聚类的所有点</span></span><br><span class="line">		  <span class="keyword">for</span> (std::vector&lt;<span class="type">int</span>&gt;::const_iterator pit = it-&gt;indices.<span class="built_in">begin</span> (); pit != it-&gt;indices.<span class="built_in">end</span> (); ++pit)</span><br><span class="line">		  &#123;</span><br><span class="line">			  cloud_cluster_don-&gt;points.<span class="built_in">push_back</span> (doncloud-&gt;points[*pit]);</span><br><span class="line">		  &#125;</span><br><span class="line">		  <span class="comment">// 设置点云元数据</span></span><br><span class="line">		  cloud_cluster_don-&gt;width = <span class="built_in">int</span> (cloud_cluster_don-&gt;points.<span class="built_in">size</span> ());</span><br><span class="line">		  cloud_cluster_don-&gt;height = <span class="number">1</span>;</span><br><span class="line">		  cloud_cluster_don-&gt;is_dense = <span class="literal">true</span>;</span><br><span class="line">		  <span class="comment">//Save cluster</span></span><br><span class="line">		  cout &lt;&lt; <span class="string">&quot;PointCloud representing the Cluster: &quot;</span> &lt;&lt; cloud_cluster_don-&gt;points.<span class="built_in">size</span> () &lt;&lt; <span class="string">&quot; data points.&quot;</span> &lt;&lt; std::endl;</span><br><span class="line">		  stringstream ss;</span><br><span class="line">		  ss &lt;&lt; <span class="string">&quot;don_cluster_&quot;</span> &lt;&lt; j &lt;&lt; <span class="string">&quot;.pcd&quot;</span>;</span><br><span class="line">		  writer.<span class="built_in">write</span>&lt;pcl::PointNormal&gt; (ss.<span class="built_in">str</span> (), *cloud_cluster_don, <span class="literal">false</span>); <span class="comment">// 保存每个聚类</span></span><br><span class="line">	  &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="https://ckyfi9zero.github.io/picx-images-hosting/20250805/image.32i9ze4x8s.webp" alt="image"></p>
<p><img src="https://ckyfi9zero.github.io/picx-images-hosting/20250805/image.1zikoi9q37.webp" alt="image"></p>
<p><img src="https://ckyfi9zero.github.io/picx-images-hosting/20250805/image.96a21u8foi.webp" alt="image"></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="https://ckyfi9zero.github.io">Fi9zero</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="https://ckyfi9zero.github.io/2025/08/05/2025-08-05-%E5%9F%BA%E4%BA%8E%E6%B3%95%E7%BA%BF%E5%BE%AE%E5%88%86%E7%9A%84%E5%88%86%E5%89%B2/">https://ckyfi9zero.github.io/2025/08/05/2025-08-05-%E5%9F%BA%E4%BA%8E%E6%B3%95%E7%BA%BF%E5%BE%AE%E5%88%86%E7%9A%84%E5%88%86%E5%89%B2/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来源 <a href="https://ckyfi9zero.github.io" target="_blank">荒岛</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/c/">c++</a><a class="post-meta__tags" href="/tags/PCL/">PCL</a></div><div class="post-share"><div class="social-share" data-image="/image/avatar.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><a class="pagination-related  no-desc" href="/2025/08/05/2025-08-05-%E5%9F%BA%E4%BA%8E%E8%B6%85%E4%BD%93%E7%B4%A0%E7%9A%84%E7%82%B9%E4%BA%91%E5%88%86%E5%89%B2/" title="基于超体素的点云分割"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info"><div class="info-1"><div class="info-item-1">上一篇</div><div class="info-item-2">基于超体素的点云分割</div></div></div></a><a class="pagination-related  no-desc" href="/2025/08/05/2025-08-05-%E5%9F%BA%E4%BA%8E%E6%9C%80%E5%B0%8F%E5%89%B2%E7%9A%84%E5%88%86%E5%89%B2/" title="基于最小割的分割"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-right"><div class="info-1"><div class="info-item-1">下一篇</div><div class="info-item-2">基于最小割的分割</div></div></div></a></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><a class="pagination-related no-desc" href="/2025/07/31/2025-07-31-%E7%94%A8kdtree%E5%AE%9E%E7%8E%B0%E5%BF%AB%E9%80%9F%E9%A2%86%E5%9F%9F%E6%90%9C%E7%B4%A2/" title="用kdtree实现快速领域搜索"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2025-07-31</div><div class="info-item-2">用kdtree实现快速领域搜索</div></div></div></a><a class="pagination-related no-desc" href="/2025/07/31/2025-07-31-%E7%94%A8%E5%85%AB%E5%8F%89%E6%A0%91%E8%BF%9B%E8%A1%8C%E6%97%A0%E5%BA%8F%E7%82%B9%E4%BA%91%E6%95%B0%E6%8D%AE%E7%9A%84%E7%A9%BA%E9%97%B4%E5%8F%98%E5%8C%96%E6%A3%80%E6%B5%8B/" title="用八叉树进行无序点云数据的空间变化检测"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2025-07-31</div><div class="info-item-2">用八叉树进行无序点云数据的空间变化检测</div></div></div></a><a class="pagination-related no-desc" href="/2025/07/31/2025-07-31-%E7%94%A8%E7%9B%B4%E9%80%9A%E6%BB%A4%E6%B3%A2%E5%99%A8%E5%AF%B9%E7%82%B9%E4%BA%91%E8%BF%9B%E8%A1%8C%E6%BB%A4%E6%B3%A2%E5%A4%84%E7%90%86/" title="用直通滤波器对点云进行滤波处理"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2025-07-31</div><div class="info-item-2">用直通滤波器对点云进行滤波处理</div></div></div></a><a class="pagination-related no-desc" href="/2025/08/01/2025-08-01-%E4%BB%8E%E7%82%B9%E4%BA%91%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AA%E6%B7%B1%E5%BA%A6%E5%9B%BE%E5%83%8F/" title="从点云创建一个深度图像"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2025-08-01</div><div class="info-item-2">从点云创建一个深度图像</div></div></div></a><a class="pagination-related no-desc" href="/2025/08/01/2025-08-01-%E4%BB%8E%E4%B8%80%E4%B8%AA%E7%82%B9%E4%BA%91%E4%B8%AD%E6%8F%90%E5%8F%96%E4%B8%80%E4%B8%AA%E5%AD%90%E9%9B%86/" title="从一个点云中提取一个子集"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2025-08-01</div><div class="info-item-2">从一个点云中提取一个子集</div></div></div></a><a class="pagination-related no-desc" href="/2025/08/01/2025-08-01-%E5%9C%A8%E6%BB%A4%E6%B3%A2%E5%99%A8%E6%A8%A1%E5%9D%97%E7%A7%BB%E9%99%A4%E7%A6%BB%E7%BE%A4%E7%82%B9/" title="在滤波器模块移除离群点"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2025-08-01</div><div class="info-item-2">在滤波器模块移除离群点</div></div></div></a></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info text-center"><div class="avatar-img"><img src="/image/avatar.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info-name">Fi9zero</div><div class="author-info-description"></div><div class="site-data"><a href="/archives/"><div class="headline">文章</div><div class="length-num">54</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">15</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">8</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/ckyFi9zero"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons"><a class="social-icon" href="https://github.com/ckyFi9zero" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:2539894206@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%9F%BA%E4%BA%8E%E6%B3%95%E7%BA%BF%E5%BE%AE%E5%88%86%E7%9A%84%E5%88%86%E5%89%B2"><span class="toc-number">1.</span> <span class="toc-text">基于法线微分的分割</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%F0%9F%94%B9-1-DoN-Difference-of-Normals-%E7%AE%97%E6%B3%95%E5%8E%9F%E7%90%86"><span class="toc-number">1.1.</span> <span class="toc-text">🔹 1. DoN (Difference of Normals) 算法原理</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%F0%9F%94%B9-2-%E5%85%B3%E9%94%AE%E5%8F%82%E6%95%B0%E8%AF%A6%E8%A7%A3"><span class="toc-number">1.2.</span> <span class="toc-text">🔹 2. 关键参数详解</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%F0%9F%94%B9-3-%E6%A0%B8%E5%BF%83%E5%A4%84%E7%90%86%E6%B5%81%E7%A8%8B"><span class="toc-number">1.3.</span> <span class="toc-text">🔹 3. 核心处理流程</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%F0%9F%94%B9-4-%E4%BB%A3%E7%A0%81%E4%BF%AE%E6%AD%A3%E4%B8%8E%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9"><span class="toc-number">1.4.</span> <span class="toc-text">🔹 4. 代码修正与注意事项</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%F0%9F%94%B9-5-%E5%8F%AF%E8%A7%86%E5%8C%96%E6%8A%80%E5%B7%A7"><span class="toc-number">1.5.</span> <span class="toc-text">🔹 5. 可视化技巧</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%F0%9F%94%B9-6-%E5%85%B8%E5%9E%8B%E5%BA%94%E7%94%A8%E5%9C%BA%E6%99%AF"><span class="toc-number">1.6.</span> <span class="toc-text">🔹 6. 典型应用场景</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81%E5%AE%9E%E7%8E%B0"><span class="toc-number">1.7.</span> <span class="toc-text">代码实现</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2025/10/05/2025-10-05-lio_sam/" title="lio_sam">lio_sam</a><time datetime="2025-10-05T02:43:51.000Z" title="发表于 2025-10-05 10:43:51">2025-10-05</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2025/08/12/2025-08-12-%E4%BD%BF%E7%94%A8docker%E6%90%AD%E5%BB%BApytorch%E7%8E%AF%E5%A2%83/" title="使用docker搭建pytorch环境">使用docker搭建pytorch环境</a><time datetime="2025-08-12T12:43:33.000Z" title="发表于 2025-08-12 20:43:33">2025-08-12</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2025/08/05/2025-08-05-docker/" title="docker">docker</a><time datetime="2025-08-05T11:02:36.000Z" title="发表于 2025-08-05 19:02:36">2025-08-05</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2025/08/05/2025-08-05-%E8%BF%90%E5%8A%A8%E5%AF%B9%E8%B1%A1%E5%88%86%E5%89%B2%E4%B8%8E%E9%85%8D%E5%87%86%E7%AE%97%E6%B3%95/" title="运动对象分割与配准算法">运动对象分割与配准算法</a><time datetime="2025-08-05T08:16:02.000Z" title="发表于 2025-08-05 16:16:02">2025-08-05</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2025/08/05/2025-08-05-%E6%9D%A1%E4%BB%B6%E6%AC%A7%E5%BC%8F%E8%81%9A%E7%B1%BB%E5%88%86%E5%89%B2/" title="条件欧式聚类分割">条件欧式聚类分割</a><time datetime="2025-08-05T07:56:39.000Z" title="发表于 2025-08-05 15:56:39">2025-08-05</time></div></div></div></div></div></div></main><footer id="footer"><div class="footer-other"><div class="footer-copyright"><span class="copyright">&copy;&nbsp;2025 By Fi9zero</span><span class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo 7.3.0</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly 5.4.2</a></span></div><div class="footer_custom_text">Only light can attract bugs.</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="日间和夜间模式切换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><div class="js-pjax"><script>(() => {
  const loadMathjax = () => {
    if (!window.MathJax) {
      window.MathJax = {
        tex: {
          inlineMath: [['$', '$'], ['\\(', '\\)']],
          tags: 'none',
        },
        chtml: {
          scale: 1.1
        },
        options: {
          enableMenu: true,
          renderActions: {
            findScript: [10, doc => {
              for (const node of document.querySelectorAll('script[type^="math/tex"]')) {
                const display = !!node.type.match(/; *mode=display/)
                const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display)
                const text = document.createTextNode('')
                node.parentNode.replaceChild(text, node)
                math.start = {node: text, delim: '', n: 0}
                math.end = {node: text, delim: '', n: 0}
                doc.math.push(math)
              }
            }, '']
          }
        }
      }

      const script = document.createElement('script')
      script.src = 'https://cdn.jsdelivr.net/npm/mathjax/es5/tex-mml-chtml.min.js'
      script.id = 'MathJax-script'
      script.async = true
      document.head.appendChild(script)
    } else {
      MathJax.startup.document.state(0)
      MathJax.texReset()
      MathJax.typesetPromise()
    }
  }

  btf.addGlobalFn('encrypt', loadMathjax, 'mathjax')
  window.pjax ? loadMathjax() : window.addEventListener('load', loadMathjax)
})()</script><script>(() => {
  const runMermaid = ele => {
    window.loadMermaid = true
    const theme = document.documentElement.getAttribute('data-theme') === 'dark' ? 'dark' : 'default'

    ele.forEach((item, index) => {
      const mermaidSrc = item.firstElementChild
      const mermaidThemeConfig = `%%{init:{ 'theme':'${theme}'}}%%\n`
      const mermaidID = `mermaid-${index}`
      const mermaidDefinition = mermaidThemeConfig + mermaidSrc.textContent

      const renderFn = mermaid.render(mermaidID, mermaidDefinition)
      const renderMermaid = svg => {
        mermaidSrc.insertAdjacentHTML('afterend', svg)
      }

      // mermaid v9 and v10 compatibility
      typeof renderFn === 'string' ? renderMermaid(renderFn) : renderFn.then(({ svg }) => renderMermaid(svg))
    })
  }

  const codeToMermaid = () => {
    const codeMermaidEle = document.querySelectorAll('pre > code.mermaid')
    if (codeMermaidEle.length === 0) return

    codeMermaidEle.forEach(ele => {
      const preEle = document.createElement('pre')
      preEle.className = 'mermaid-src'
      preEle.hidden = true
      preEle.textContent = ele.textContent
      const newEle = document.createElement('div')
      newEle.className = 'mermaid-wrap'
      newEle.appendChild(preEle)
      ele.parentNode.replaceWith(newEle)
    })
  }

  const loadMermaid = () => {
    if (true) codeToMermaid()
    const $mermaid = document.querySelectorAll('#article-container .mermaid-wrap')
    if ($mermaid.length === 0) return

    const runMermaidFn = () => runMermaid($mermaid)
    btf.addGlobalFn('themeChange', runMermaidFn, 'mermaid')
    window.loadMermaid ? runMermaidFn() : btf.getScript('https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js').then(runMermaidFn)
  }

  btf.addGlobalFn('encrypt', loadMermaid, 'mermaid')
  window.pjax ? loadMermaid() : document.addEventListener('DOMContentLoaded', loadMermaid)
})()</script></div><script defer src="https://npm.elemecdn.com/jquery@latest/dist/jquery.min.js"></script><script defer data-pjax src="/js/pig.js"></script><script src="/js/weather.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="text-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="站内搜索..." type="text"/></div></div><hr/><div id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js"></script></div></div></body></html>